package com.proxy.demo2;

import java.lang.reflect.Method;

import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

public class CglibProxy implements MethodInterceptor{

	private Enhancer enhancer = new Enhancer();  
	public Object getProxy(Class clazz){
		//设置需要创建子类的类 
		enhancer.setSuperclass(clazz);
		//通过字节码技术动态创建子类实例  
		enhancer.setCallback(this);
		return enhancer.create();
	}
	@Override
	//实现MethodInterceptor接口方法
	public Object intercept(Object arg0, Method arg1, Object[] arg2, MethodProxy arg3) throws Throwable {
		System.out.println("before;方法名："+arg1.getName());
		//通过代理类调用父类中的方法  
		Object result = arg3.invokeSuper(arg0, arg2);
		System.out.println("after");
		return result;
	}

}
